package com.base.chat.ac;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.RecoverySystem;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.chat.R;
import com.base.chat.bean.NewFriendsBean;
import com.base.chat.databinding.ActivityNewfriendsListBinding;
import com.base.chat.presenter.NewFriendsListPresenter;
import com.base.chat.view.NewFriendsListView;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;
import com.base.commonlib.utils.GlideUtil;
import com.base.commonlib.utils.ToastUtil;
import com.base.commonlib.widget.OvalImageView;
import com.othershe.combinebitmap.CombineBitmap;
import com.othershe.combinebitmap.layout.DingLayoutManager;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/chat/new_group_chat")
public class NewGroupChatActivity extends CBaseBarMvpActivity<NewFriendsListPresenter> implements NewFriendsListView {
    private ActivityNewfriendsListBinding mBinding;
    private Integer page = 1;
    private BaseAdapter<NewFriendsBean.Rows> mAdapter;
    private List<NewFriendsBean.Rows> mData=new ArrayList<>();

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_newfriends_list;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityNewfriendsListBinding) mRootBinding;
        mBinding.smartRefreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.smartRefreshLayout.setHeaderHeight(60);
        mBinding.smartRefreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        mPresenter = new NewFriendsListPresenter(this, this);


        CombineBitmap.init(mContext)
                .setLayoutManager(new DingLayoutManager()) // 必选， 设置图片的组合形式，支持WechatLayoutManager、DingLayoutManager
                .setSize(10) // 必选，组合后Bitmap的尺寸，单位dp
                .setGap(2) // 单个图片之间的距离，单位dp，默认0dp
                .setGapColor(Color.parseColor("#ffffff")) // 单个图片间距的颜色，默认白色
                //.setPlaceholder() // 单个图片加载失败的默认显示图片
                .setUrls() // 要加载的图片url数组
                .setBitmaps() // 要加载的图片bitmap数组
                .setResourceIds() // 要加载的图片资源id数组
                .setImageView(new ImageView(mContext)) // 直接设置要显示图片的ImageView
                // 设置“子图片”的点击事件，需使用setImageView()，index和图片资源数组的索引对应
                .build();
    }

    @Override
    protected void start() {
        setBackNavigation();
        setBarTitle("新的群聊");
        mPresenter=new NewFriendsListPresenter(this,this);
        setRigImg(R.mipmap.icon_add_friend, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/friend/add").navigation();
            }
        });

//            mBinding.imgLeft.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    finish();
//                }
//            });
//            mBinding.imgRight.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    ARouter.getInstance().build("/friend/add").navigation();
//                }
//            });

            mBinding.search.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        //搜索
        mBinding.search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rv.setLayoutManager(linearLayoutManager);
        mAdapter = new BaseAdapter<NewFriendsBean.Rows>(mData,mContext,true) {
            @Override
            public void convert(BaseHolder holder, final NewFriendsBean.Rows rows, boolean isHeader, boolean isBottom) {
                OvalImageView it_user_img = holder.getView(R.id.it_user_img);
                TextView it_user_name = holder.getView(R.id.it_user_name);
                TextView it_agree = holder.getView(R.id.it_agree);
                TextView it_refuse = holder.getView(R.id.it_refuse);
                GlideUtil.load(mContext,rows.getAvatar(),it_user_img);
                it_user_name.setText(rows.getUserName());
                it_agree.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.agreeFriend(rows.getFriendApplyId(),"AGREE");
                    }
                });

                it_refuse.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.agreeFriend(rows.getFriendApplyId(),"REJECT");
                    }
                });
            }
        };
        mAdapter.putNormalType(R.layout.item_new_friends);
        mBinding.rv.setAdapter(mAdapter);
        mBinding.smartRefreshLayout.autoRefresh();
        mBinding.smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                mPresenter.getList(page,20);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                mPresenter.getList(page,20);
            }
        });

    }

    @Override
    public void returnNoData() {

    }

    @Override
    public void returnList(NewFriendsBean bean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.clear();
        mData.addAll(bean.getRows());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnMoreList(NewFriendsBean bean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.addAll(bean
                .getRows());
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void returnNoMoreData() {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }

    @Override
    public void returnError() {
        mBinding.empty.setVisibility(View.GONE);
        if (page == 1) {
            mBinding.smartRefreshLayout.finishRefresh();
        } else {
            mBinding.smartRefreshLayout.finishLoadmore();
        }
    }

    @Override
    public void returnHandleAgreeFriendSuccess(String msg) {
        ToastUtil.show(msg);
    }

    @Override
    public void returnHandleAgreeFriendError(String msg) {
        ToastUtil.show(msg);
    }

    @Override
    public void returnHandleRefuseFriendSuccess(String msg) {
        ToastUtil.show(msg);
    }

    @Override
    public void returnHandleRefuseFriendError(String msg) {
        ToastUtil.show(msg);
    }
}
